package com.htfyun.factorytest.codec;

import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

/**
 * pcm音频解码
 *
 * @author 奎华
 */
public class PcmCodec extends BaseCodec {

    private static PcmCodec instance;

    public static PcmCodec getInstance() {
        if (null == instance) {
            instance = new PcmCodec();
        }
        return instance;
    }

    private InputStream mInputStream;
    private final String tag = PcmCodec.class.getSimpleName();

    @Override
    public Boolean decode(InputStream ins) {//文件输入流
        // TODO Auto-generated method stub
        mInputStream = ins;
        return true;
    }

    @Override
    public byte[] fetchData(int datasize) {//从输入流中获取指定大小的数据
        // TODO Auto-generated method stub
        if (mInputStream != null) {
            byte[] data = new byte[datasize];
            try {
                int result = mInputStream.read(data);
                if (result > 0 && result < datasize) {
                    byte[] temp = new byte[result];
                    System.arraycopy(data, 0, temp, 0, result);
                    data = null;
                    return temp;
                } else if (result < 0) {//到文件末尾
                    data = null;
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return data;
        } else {
            Log.e(tag, "输入流为空");
            return null;
        }
    }

    @Override
    public void release() {
        // TODO Auto-generated method stub
        mInputStream = null;
    }

}
